<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
//tao function ket noi

include('clsketnoi.php');

function taomacuonsach()
{
    return taomatudong("select macuonsach from tblcuonsach","macuonsach",2,14);
}

//them du lieu
function themcuonsach()
{
    $macuonsach = $_POST['macuonsach'];
    $madausach = $_POST['madausach'];
    $trangthai = $_POST['trangthai'];
    $tinhtrang = $_POST['tinhtrang'];
    mysql_query("insert into tblcuonsach (macuonsach, madausach, trangthai, tinhtrang) values ('$macuonsach','$madausach','$trangthai','$tinhtrang')",dbketnoi());
    mysql_close();
}
//xoa 
function xoacuonsach()
{
    $macuonsach= $_POST['macuonsach'];
    $query = mysql_query("SELECT tblcuonsach.macuonsach as masach, maphieughinhan, mactptls, mactmt FROM tblcuonsach, tblghinhanmatsach, tblchitietphieumuontra".
    ", tblchitietphieuthanhlysach where tblcuonsach.macuonsach=tblghinhanmatsach.macuonsach and tblcuonsach.macuonsach=tblchitietphieumuontra.macuonsach".
    " and tblcuonsach.macuonsach=tblchitietphieuthanhlysach.macuonsach",dbketnoi());
    $i=0;
    while($result = mysql_fetch_array($query))
    {
        if($result['masach']==$macuonsach)
        {
            $q= mysql_query("delete from tblghinhanmatsach where maphieughinhan='".$result['maphieughinhan']."'", dbketnoi());
            mysql_free_result($q);
            $x=mysql_query("delete from tblchitietphieuthanhlysach where mactptls='".$result['mactptls']."'", dbketnoi());
            mysql_free_result($x);
            $y=mysql_query("delete from tblchitietphieumuontra  where mactmt='".$result['mactmt']."'", dbketnoi());
            mysql_free_result($y);
        }
        $i++;
    }
    mysql_query("delete from tblcuonsach where macuonsach='$macuonsach'",dbketnoi());
    mysql_close();
}
//update 
function updatecuonsach()
{
    $macuonsach = $_POST['macuonsach'];
    $madausach = $_POST['madausach'];
    $trangthai = $_POST['trangthai'];
    $tinhtrang = $_POST['tinhtrang'];
    mysql_query("update tblcuonsach set madausach='$madausach', trangthai='$trangthai', tinhtrang='$tinhtrang' where macuonsach='$macuonsach'",dbketnoi());
    mysql_close();
}

// hien thi du lieu
function hienthicuonsach()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT macuonsach, tblcuonsach.madausach as madausach, trangthai, tinhtrang, tbldausach.ten as tendausach from tblcuonsach, tbldausach where"."
                tbldausach.madausach=tblcuonsach.madausach",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodiencuonsach.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo "<td><a href='' >".$result['macuonsach']."</a></td>";
            echo "<td>".$result['madausach']."</td>";
            echo "<td>".$result['tendausach']."</td>";
            echo "<td>".$result['trangthai']."</td>";
            echo "<td>".$result['tinhtrang']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã Cuốn Sách ","Mã Đầu Sách","Tên Đầu Sách","Trạng Thái", "Tình Trạng");
    echo "<table style='border-color:black;'  cellspacing='1' width='500px'>";
    echo "<tr>";
    for($i=0; $i<=4; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}

?>
